﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace SystemUpdatesCore.Server
{
    /// <summary>
    /// 服务端版本
    /// </summary>
    public class ServerVersion : ServerFiles, IServer
    {
        public ServerVersion() : base()
        {
            GetUrl();
        }

        private List<ServerUrlModel> urls =
            new List<ServerUrlModel>(){
                new ServerUrlModel(1, "https://gitee.com/ypeuee/MouseKeyMacro.Simple.Publish/raw/master/version " ),
                new ServerUrlModel(2, "https://gitee.com/ypeuee/MouseKeyMacro.Simple.Publish/raw/master/version " )
             };

        /// <summary>
        /// 获取版本地址组
        /// </summary>
        public List<ServerUrlModel> Urls
        {
            get { return urls; }
            set { urls = value; }
        }

        private ServerUrlModel url;
        /// <summary>
        /// 获取版本当前可用的URL
        /// </summary>
        public ServerUrlModel Url
        {
            get { return url; }
            set { url = value; }
        }

        /// <summary>
        /// 获取新可用地址
        /// </summary>
        /// <returns></returns>
        ServerUrlModel GetUrl()
        {
            url = Urls.Where(m => m.IsSuccess != false).OrderBy(m => m.Index).FirstOrDefault();
            return url;
        }

        /// <summary>
        /// 设计地址状态
        /// </summary>
        /// <param name="url"></param>
        void SetUrl(ServerUrlModel url)
        {
            var v = Urls.Where(m => m.Url == url.Url).FirstOrDefault();
            if (v != null)
            {
                v.Index = url.Index;
                v.IsSuccess = url.IsSuccess;
            }
        }


        /// <summary>
        /// 获取当前版本号
        /// 尝试所有配置的URL
        /// </summary>
        /// <param name="systemName"></param>
        /// <returns></returns>
        public async Task<string> GetVersionNum(string systemName)
        {
            var ser = await GetVersion(systemName);
            return ser.Version;
        }

        /// <summary>
        /// 获取当前版
        /// 尝试所有配置的URL
        /// </summary>
        /// <param name="systemName"></param>
        /// <returns></returns>
        public async Task<ServerVersionModel> GetVersion(string systemName)
        {
            ///尝试多个地址
            while (Url != null)
            {
                try
                {
                    return await GetVersion(systemName, Url.Url);
                }
                catch (Exception ex)
                {
                    Url.IsSuccess = false;
                    SetUrl(Url);
                    GetUrl();
                }
            }

            return null;
        }


        /// <summary>
        /// 获取当前版本号,从指定的地址。
        /// </summary>
        /// <param name="systemName"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        async Task<string> GetVersionNum(string systemName, string address)
        {
            ServerVersionModel version = await GetVersion(systemName, address);

            return version?.Version;
        }

        /// <summary>
        /// 获取当前版本号,从指定的地址。
        /// </summary>
        /// <param name="systemName"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        async Task<ServerVersionModel> GetVersion(string systemName, string address)
        {
            var str = await networkClient.DownloadStringTaskAsync(address);

            ServerVersionModel versions = str.ToObject<ServerVersionModel>();
            var version = versions.SystemName == systemName ? versions : null;
            return version;
        }
    }


}
